package com.zide.ams.service.impl;

import java.util.Arrays;
import java.util.List;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zide.ams.domain.po.AmsCategory;
import com.zide.ams.domain.vo.result.AmsCategoryResultVO;
import com.zide.ams.domain.vo.save.AmsCategorySaveVO;
import com.zide.ams.domain.vo.search.AmsCategorySearchVO;
import com.zide.ams.mapper.AmsCategoryMapper;
import com.zide.ams.service.IAmsCategoryService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.zide.common.core.vo.CommonParamIdVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 文章分类Service业务层处理
 * 
 * @author zide
 * @date 2024-04-29
 */
@Slf4j
@Service
public class AmsCategoryServiceImpl implements IAmsCategoryService {
    @Resource
    private AmsCategoryMapper amsCategoryMapper;

    /**
     * 查询文章分类列表
     */
    @Override
    public List<AmsCategoryResultVO> searchAmsCategoryList(AmsCategorySearchVO searchVO) {
        LambdaQueryWrapper<AmsCategory> wrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotEmpty(searchVO.getName())) {
            wrapper.like(AmsCategory::getName,searchVO.getName());
        }
        List<AmsCategory> amsCategories = amsCategoryMapper.selectList(wrapper);

        return BeanUtil.copyToList(amsCategories,AmsCategoryResultVO.class);
    }

    /**
     * 查询文章分类详情
     */
    @Override
    public AmsCategoryResultVO selectAmsCategoryById(Long id) {
            AmsCategory dbInfo = amsCategoryMapper.selectById(id);
            AmsCategoryResultVO result = BeanUtil.copyProperties(dbInfo,AmsCategoryResultVO.class);
        return result;
    }

    /**
     * 新增文章分类
     */
    @Override
    public int saveAmsCategory(AmsCategorySaveVO saveVO) {
        AmsCategory insertBean = BeanUtil.copyProperties(saveVO,AmsCategory.class);
        return amsCategoryMapper.insert(insertBean);
    }

    /**
     * 修改文章分类
     */
    @Override
    public int updateAmsCategory(AmsCategorySaveVO saveVO) {
        AmsCategory updateBean = BeanUtil.copyProperties(saveVO,AmsCategory.class);
        return amsCategoryMapper.updateById(updateBean);
    }

    /**
     * 批量删除文章分类
     */
    @Override
    public int removeAmsCategoryByIds(CommonParamIdVO idVO) {
        if(ArrayUtil.isNotEmpty(idVO.getIds())) {
            return amsCategoryMapper.deleteBatchIds(Arrays.asList(idVO.getIds()));
        }else {
            return amsCategoryMapper.deleteById(idVO.getId());
        }

    }

    @Override
    public int updateStatus(AmsCategorySaveVO saveVO) {
        AmsCategory updateBean = BeanUtil.copyProperties(saveVO,AmsCategory.class);
        return amsCategoryMapper.updateById(updateBean);
    }
}
